package com.person.jc.tag;

import java.util.List;

import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.jfinal.plugin.activerecord.Record;
import com.person.jc.utils.SessionKit;
import com.person.jc.utils.Utils;


public class FunMenuTag extends BodyTagSupport {
    private static final long serialVersionUID = 1L;

    private String menuId;
    
    /**
     * 
     * @Description: [判断按钮上文字是否为2个字，如果为两个字则中间加空格]
     * @Author: 武智雄 (wuzhixiong@vvise.com)
     * @Date: 2014-5-8
     * @@param funName
     * @@return
     */
    private String getFunName(String funName){
        if(Utils.isNotEmpty(funName) && funName.length() == 2){
            funName = funName.substring(0, 1) + "&nbsp;" + funName.substring(1);
        }
        
        return funName;
    }
    
    @SuppressWarnings("unchecked")
    public int doEndTag() throws JspException {
        JspWriter out = pageContext.getOut();
        try {
            StringBuffer outStr = new StringBuffer("");
            if(Utils.isNotEmpty(menuId)){

                String searchBtn = "";
                String funBtn = "";
                outStr.append("<div class=\"message2\">");
                outStr.append("<div class=\"btn\"><ul>");

                if(menuId.equals("sel_search_btn")){
                    searchBtn = "<li class=\"search_btn\"  onclick=\"search()\"><a href=\"javascript:void(0)\">查&nbsp;询</a></li>";
                }else{

                    //List<Record> list =
                    HttpSession session = SessionKit.get();
                    List<Record> list = (List<Record>)session.getAttribute("funList");

                    for(Record map : list){
                        if(menuId.equals(String.valueOf(map.get("p_menu_id")))){
                            String fn = String.valueOf(map.get("url"));
                            String clazz = String.valueOf(map.get("menu_class"));
                            if(Utils.isNotEmpty(fn) && fn.indexOf("(") == -1)
                                fn += "()";
                            String name = getFunName(String.valueOf(map.get("menu_name")));
                            if("search_btn".equals(clazz)){
                                searchBtn = "<li class=\""+Utils.convertNullToEmpty(clazz)+"\"  onclick=\""+ fn +"\"><a href=\"javascript:void(0)\">"+name+"</a></li>";
                            }else{
                                funBtn += "<li  class=\""+Utils.convertNullToEmpty(clazz)+"\"   onclick=\""+ fn +"\"><a href=\"javascript:void(0)\">"+name+"</a></li>";
                            }
                        }
                    }
                }


                outStr.append(funBtn + searchBtn);
                outStr.append(" </ul></div></div>");
            }
            out.write(outStr.toString());
        }
        catch (Exception e) {
            throw new JspException("Error:" + e.getMessage());
        }
        return EVAL_BODY_INCLUDE;
    }
    
    public String getMenuId() {
        return menuId;
    }
    public void setMenuId(String menuId) {
        this.menuId = menuId;
    }
    
}
